package com.flycent.hqhs.adapter;

import java.util.List;

import net.tsz.afinal.FinalBitmap;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.flycent.hqhs.App;
import com.flycent.hqhs.R;
import com.flycent.hqhs.bean.MainTopAdvertisement;
import com.flycent.hqhs.ui.ProductDetailActivity;
import com.flycent.hqhs.util.StringUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author fbl
 *  首页广告栏的适配器
 */
public class ImageAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private static String[] imagePath;
	private List<MainTopAdvertisement.AdServing> mAdServings;
	FinalBitmap fb;

	
	public ImageAdapter(Context context,List<MainTopAdvertisement.AdServing>mAdServing){
		fb = FinalBitmap.create(context);
		mContext = context;
		mAdServings = mAdServing;
		imagePath = new String[mAdServings.size()];
		for (int i = 0; i < mAdServings.size(); i++) {
			imagePath[i] = mAdServings.get(i).getAdMaterial().getPath();
		}
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;   //返回很大的值使得getView中的position不断增大来实现循环
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_main_flow_image, null);
		}


		ImageView image1 = (ImageView) convertView.findViewById(R.id.imgView);
		if(imagePath.length > 0){
			ImageLoader.getInstance().displayImage(imagePath[position%imagePath.length], image1);
//			fb.display(image1, imagePath[position%imagePath.length]);
		}
		

		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				App.showShortToast("你点击了="+ position%imagePath.length);
				int i = position%mAdServings.size();
				String url = mAdServings.get(i).getAdMaterial().getUrl();
				if(url != null && url.length() != 0)
					toMatch(url);

//				ImageUtil.imageBrower(mContext,0, com.flycent.hqhs.Constants.images);

				/*
				Intent intent = new Intent(mContext,DetailActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("image_id", ids[position%ids.length]);

				intent.putExtras(bundle);
				mContext.startActivity(intent);
				 */

			}
		});
		return convertView;
	}

	private void toMatch(String str) {
		
		if(StringUtil.isProduct(str)){
			String id = StringUtil.getProductId(str);
			Intent intent = new Intent(mContext,ProductDetailActivity.class);
			intent.putExtra("productID", id);
			mContext.startActivity(intent);
			
//			App.showShortToast("商品");
		}else if(StringUtil.isList(str)){
			App.showShortToast("一级列表");
		}else if(StringUtil.isCateList(str)){
			App.showShortToast("二级列表");
		}

	}


}
